#include "Map.hh"
#include <iostream>
#include <random>

Map::Map() {
    // std::make_unique 是一种创建 std::unique_ptr 的推荐方式，它确保我们不会在对象创建时发生内存泄漏。
    // std::make_unique is a recommended way to create a std::unique_ptr, ensuring no memory leaks when creating the object.
    currentRoom = std::make_unique<RestRoom>("Starting Room", "A calm place to begin your journey.");
}

Room* Map::getCurrentRoom() const {
    return currentRoom.get();
}

int Map::getCurrentLayer() const {
    return currentLayer;
}

int Map::getRoomsVisitedThisLayer() const {
    return roomsVisitedThisLayer;
}

void Map::moveInDirection(Player& player, bool forward) {
    if (player.getAP() <= 0) {
        std::cout << "[No AP left. End your turn to recover.]\n";
        return;
    }

    roomsVisitedThisLayer++;
    if (roomsVisitedThisLayer >= 6 && currentLayer < 3) {
        std::cout << "[You found a staircase leading up...]\n";
        currentLayer++;
        roomsVisitedThisLayer = 0;
        currentRoom = std::make_unique<RestRoom>("Staircase Room", "You feel the pull of progress.");
        player.setAP(player.getAP() - 1);
    } else if (currentLayer == 3 && roomsVisitedThisLayer >= 4) {
        currentRoom = std::make_unique<BossRoom>("Boss Room", "The final enemy awaits.");
        player.setAP(0);
    } else {
        currentRoom = generateRandomRoom();
        player.setAP(player.getAP() - 1);
    }
    currentRoom->descriptionText();
    currentRoom->enterRoom(player);
}

std::unique_ptr<Room> Map::generateRandomRoom() {
    static std::random_device rd;//这是一个随机数种子生成器，用于提供比较随机的种子值。
                                // This is a random number seed generator, used to provide a relatively random seed value.
    static std::mt19937 gen(rd());//std::mt19937 是一个伪随机数生成器，使用来自 rd() 的种子值进行初始化，确保生成的随机数序列具有较好的随机性。
                                //std::mt19937 is a pseudo-random number generator, initialized with the seed value from rd() to ensure good randomness in the generated random number sequence.
    std::uniform_int_distribution<> dist(1, 100);//创建一个整数分布对象 dist，其作用是：在 [1, 100] 范围内随机等概率地生成整数。
                                                //std::uniform_int_distribution<> is an integer distribution object, ensuring that generated integers are evenly distributed within the specified range [1, 100].
    int roll = dist(gen);// 使用随机数生成器和分布生成一个随机整数，并将其存储在 roll 变量中。
                        //This generates a random integer using the dist distribution object and the gen random number generator, storing the result in the roll variable.

    if (roll <= 40) {
        return std::make_unique<MonsterRoom>("Monster Room", "Dangerous creatures lurk here.");
    } else if (roll <= 50) {
        return std::make_unique<ShopRoom>("Shop", "Trade gold for power.");
    } else if (roll <= 60){
        return std::make_unique<RestRoom>("Rest Room", "Time to recover.");
    } else if (roll <= 70){
        return std::make_unique<ForgeRoom>("Forge Room", "You can forge you items here.");
    } else if (roll <= 80){
        return std::make_unique<TreasureRoom>("TreasureRoom", "The room has treasures.");
    } else{
        return std::make_unique<LotteryRoom>("LotteryRoom", "Pay glod and get random prize.");
    }
}

bool Map::tryAPReset(Player& player) {
    if (apResetUsed < maxApReset) {
        apResetUsed++;
        player.setAP(player.getmaxAP());
        std::cout << "[AP Reset used (" << apResetUsed << "/" << maxApReset << "). AP restored.]\n";
        return true;
    } else {
        std::cout << "[No AP resets remaining.]\n";
        return false;
    }
}

bool Map::allAPResetsUsed() const {
    return apResetUsed >= maxApReset;
}
